﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Settings
{
    /// <summary>
    /// This object represents the methods of a LanguageDAO.
    /// </summary>
    internal class LanguageDAO
    {

        #region general

        private static Language LoadObj(IDataReader datareader)
        {
            Language obj = new Language();
            obj.LanguageCode = MpDataHelp.GetString(datareader, "LanguageCode");
            obj.LanguageTitle = MpDataHelp.GetString(datareader, "LanguageTitle");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            return obj;
        }

        #endregion

        public static int Language_Add(Language obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Language_Add");
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "LanguageTitle", DbType.String, obj.LanguageTitle);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@LanguageCode"));
            return -1;
        }
        public static int Language_Update(Language obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Language_Update");
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "LanguageTitle", DbType.String, obj.LanguageTitle);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Language_Delete(string LanguageCode)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Language_Delete");
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, LanguageCode);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Language> Language_Select(string LanguageCode)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Language_Select");
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, LanguageCode);
            List<Language> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Language>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}

